package org.pubflex.tests{

      import org.pubflex.ContentDisplayChooser;
      import flash.events.Event;
      import flash.display.DisplayObject;
      import flexunit.framework.*;      
      import mx.rpc.events.ResultEvent;
      import mx.rpc.http.mxml.HTTPService;
                	         
      public class PubFlexTest extends TestCase {
  		
  	    public function PubFlexTest( methodName:String ) {
   			super( methodName );
        }
  	
  		public static function suite():TestSuite {
   			var ts:TestSuite = new TestSuite();
        	//ts.addTest( new PubFlexTest( "testgetDisplayObjectBasedOnType" ) );
        	ts.addTest( new XmlTest( "testEntryCreation" ) );
   			return ts;
   		}
  		
  		public function testgetDisplayObjectBasedOnType():void{    
            var asyncCall:Function = addAsync(handleFeedResult, 1000);
            var httpService:HTTPService = new HTTPService();

            httpService.url = "http://bitworking.org/projects/apptestsite/app.cgi/service/entry"; 
            httpService.resultFormat = "e4x";
            httpService.addEventListener("result",asyncCall);
            
            httpService.send();
                       
  		}
  		  	
        private function handleFeedResult(event:Event, token:Object=null):void {
            
            assertTrue(event is ResultEvent); 
            var e:ResultEvent = event as ResultEvent;
            var feed:XML = e.result as XML;
            //trace("event " + event);
            //trace("tmp " + feed);
    
            namespace local = "http://www.w3.org/2007/app";
            namespace atom = "http://www.w3.org/2005/Atom"            	            	
            use namespace local;
            use namespace atom;
            	        
            var entries:XMLList = feed.entry;
            assertTrue("Failed to get any entries from the feed. " ,entries.length() > 0);
            
            var entry:XML;
            for each (entry in entries){
                 //trace("Entry " + entry);

                var a:Object = entry.content[0];   	            	
                 trace(" --> " + entry.content[0] );                 
            
                 if(entry.content != null){
                    var displayObject:DisplayObject = ContentDisplayChooser.chooseDisplay(entry.content[0]);
                    assertNotNull(displayObject);         
                 }           
            }                        
        }  		  		
  	}
}